/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package net.worlds;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.io.PrintStream;
import java.util.ArrayList;
import javax.swing.JApplet;
import javax.swing.JComponent;
import javax.swing.JDesktopPane;
import javax.swing.JScrollPane;
import javax.swing.SwingUtilities;
import javax.swing.border.EmptyBorder;
import net.worlds.ui.PartsTree;
import net.worlds.ui.PropsTable;
import net.worlds.ui.UI;


public class MainApplet extends JApplet implements Runnable{
    public static final String ICON_PLAY = "/net/worlds/resources/play.png";
    public static final String ICON_STOP = "/net/worlds/resources/stop.png";
    private GamePanel mainPanel;
    private Container view;
    private JDesktopPane desktop;
    private Thread animator;
    public static int width;
    public static int height;
    private ArrayList<Component> components = new ArrayList<Component>();
    @Override
    public String getParameter(String k)
    {
        try
        {
            return super.getParameter(k);
        }
        catch(Exception ex)
        {
            return "";
        }
    }
    @Override
    public void init()
    {
        setIgnoreRepaint(true);
        String options = this.getParameter("options");

        //options = "1 Jacob_ TVhemrASdA7U8iTqSWYdrdmmrGP7mSQYl4gR4mLBp1gfmrxFVz play false http://2dworlds.org/game/join.php?mode=play&id=1279";
        if(options != null)
        {
            String[] args = options.split(" ");
            Main.parseArgs(args);
        }
        try {
            Main.init(this);
        } catch (Exception ex) {
            Main.error(ex, true);
        }
        width = getWidth();
        height = getHeight();
        /*if(Main.canUseTools)
        {
            width = 1024;
            height = 768;
        }
        else
        {
            width = 800;
            height = 600;
        }*/
        animator = new Thread(this);
        animator.setName("2DWorldsMain");
        animator.start();
        Main.getView().onCreated();
        Main.mainThread = animator;
    }
    public void setMainPanel(GamePanel panel)
    {
        mainPanel = panel;
        mainPanel.rebuildImage();
        add(mainPanel);
        makeLayout();
        
        mainPanel.setStartSize(getWidth(), getHeight());
        mainPanel.setup();
    }
    public void makeLayout()
    {
        if(Main.canUseTools)
        {
            /*initComponents();
            mainSplitPane.setRightComponent(mainPanel);
            tableScroller.setViewportView(propsTable);
            treeScroller.setViewportView(partsTree);*/
            PrintStream out = System.out;
            PrintStream err = System.err;
            System.setOut(new PrintStream(new ChatOutputStream(false, out), true));
            System.setErr(new PrintStream(new ChatOutputStream(true, err), true));
            setLayout(new BorderLayout());
            add(mainPanel, BorderLayout.CENTER);
        }
        else
        {
            setLayout(new BorderLayout());
            add(mainPanel, BorderLayout.CENTER);
            setSize(800, 600);
        }
        view = mainPanel;
    }
    public Container addComponent(JComponent c)
    {
        final JScrollPane sp = new JScrollPane(c);
        sp.setBorder(new EmptyBorder(0, 0, 0, 0));
        sp.setSize(c.getSize());
        components.add(sp);
        SwingUtilities.invokeLater(new Runnable()
        {

            public void run() {
                mainPanel.add(sp);
                validate();
                mainPanel.repaint();
            }
        });
        return sp;
    }
    public void addContainer(final Container c)
    {
        components.add(c);
        SwingUtilities.invokeLater(new Runnable()
        {

            public void run() {
                mainPanel.add(c);
                validate();
                mainPanel.repaint();
            }
        });
    }
    public void removeContainer(final Container c)
    {
        components.remove(c);
        SwingUtilities.invokeLater(new Runnable()
        {

            public void run() {
                mainPanel.remove(c);
                validate();
            }
        });
    }
    public void removeComponent(final Container c)
    {
        components.remove(c);
        SwingUtilities.invokeLater(new Runnable()
        {

            public void run() {
                if(c != null)
                {
                    mainPanel.remove(c);
                    validate();
                }
            }
        });
    }
    public void removeComponents()
    {
        for(Component c : components)
        {
            mainPanel.remove(c);
        }
    }
    public GamePanel getGamePanel()
    {
        return mainPanel;
    }
    public void setGameView(Container p)
    {
        if(p != null)
            UI.viewVisible = false;
        else
            p = mainPanel;
        remove(view);
        add(p, BorderLayout.CENTER);
        validate();
        mainPanel.repaint();
        repaint();
        view = p;
    }
    public void hideTools()
    {
        SwingUtilities.invokeLater(new Runnable()
        {

            public void run() {
                setSize(800, 600);
                Main.setView(new EditorView());
            }
        });
    }
    public void toggleConsole()
    {
        UI.showConsole = !UI.showConsole;
    }
    public PropsTable getPropsTable()
    {
        return Main.getView().getPropsTable();
    }
    public PartsTree getPartsTree()
    {
        return Main.getView().getPartsTree();
    }
    
    @Override
    public void destroy()
    {
        if(Main.getClient() != null)
        {
            Main.getClient().disconnect();
        }
        else if(Main.getServer() != null)
        {
            Main.getServer().stop();
        }
    }

    public void run() {
        try {
            while(Thread.currentThread() == animator)
            {
                Main.gameStep();
            }
        } catch (Exception ex) {
            Main.error = true;
            System.err.println("ERROR: "+ex);
            Main.error(ex);
        }
    }
}
